﻿using System.Common.Exceptions;
using System.ComponentModel;

namespace Infinity.Common
{
	public class NotifyPropertyChanged : INotifyPropertyChanged
	{
		protected void OnPropertyChanged(string propertyName)
		{
			Ensure.NotNull("propertyName", propertyName);

			var type = this.GetType();

			foreach (var property in type.GetProperties())
			{
				if (property.Name == propertyName)
				{
					PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
					return;
				}
			}

			throw new NotImplementedPropertyException(propertyName);
		}

		protected void OnPropertyChanged(params string[] propertyNames)
		{
			Ensure.NotNull("propertyNames", propertyNames);

			foreach (var propertyName in propertyNames)
			{
				OnPropertyChanged(propertyName);
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;
	}
}